<template>
  <div class="ordering">
    <div class="heading">订单管理</div>
    <div class="query-view">
      <!--日期选择择器-->
      <div class="quotation-query">
        <div class="quotation-title">交易时间</div>
        <div class="block">
          <el-date-picker
            v-model="order_time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD"
            :size="small"
          />
        </div>
      </div>
      <!--下拉列表-->
      <div class="quotation-query">
        <div class="quotation-title">桌号</div>
        <div>
          <el-select
            v-model="sevakey"
            clearable
            placeholder="请选择桌号"
            style="width: 240px"
          >
            <el-option
              v-for="item in options"
              :key="item.table_number"
              :label="item.table_name"
              :value="item.table_number"
            />
          </el-select>
        </div>
      </div>
      <!--查询按钮-->
      <div class="quotation-query">
        <el-button type="success" @click="Searchmenu" :loading="load"
          ><el-icon><Search /></el-icon><span>查询</span>
        </el-button>
      </div>
    </div>
    <!--表格-->
    <div>
      <el-table :data="data_table" style="width: 100%">
        <el-table-column
          prop="order_time"
          label="交易时间"
          :formatter="formatTime"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          class="custom-cell"
          prop="table_number"
          label="桌号"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="number_of_diners"
          align="center"
          label="用餐人数"
          min-width="100"
          class="custom-cell"
        >
        </el-table-column>
        <el-table-column align="center" label="菜单详情" min-width="100">
          <template #default="scope">
            <!-- 将自定义类名应用到单元格 -->
            <el-button
              size="small"
              :loading="loadmsg"
              @click="Detailed_Menu(scope.$index, scope.row)"
              >详细菜单</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="price"
          align="center"
          label="交易金额"
          min-width="100"
          class="custom-cell"
        ></el-table-column>

        <el-table-column align="center" label="交易状态" min-width="100">
          <template #default="scope">
            <!-- 将自定义类名应用到单元格 -->
            <el-button
              size="small"
              type="danger"
              :disabled="false"
              v-if="scope.row.transac_status != 'paid'"
              @click="Check_Out(scope.$index, scope.row)"
              >未结账</el-button
            >
            <el-button v-else size="small" type="success" disabled
              >已结账</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          background
          layout="prev,pager,next"
          :total="totalCount"
          :hide-on-single-page="false"
          @current-change="current_change"
        >
        </el-pagination>
      </div>
      <!--弹窗-->
      <Order_details ref="orderLog" />
    </div>
  </div>
</template>

<script>
import {
  reactive,
  toRefs,
  onMounted,
  getCurrentInstance,
  watch,
  provide,
  ref,
} from "vue";
import {Search} from "@element-plus/icons-vue";
import Order_details from "./compent/order_details.vue";
export default {
  components: {Search, Order_details},

  setup() {
    // const orderlists=reactive({data:[]})
    let orderLog = ref();
    const {proxy} = getCurrentInstance();
    const datas = reactive({
      time: "", //时间
      sevakey: "", //选中的桌号
      options: [], //桌号数据
      data_table: [], //表格数据
      dialogTableVisible: false,
      totalCount: 100,
      page: 1,
      order_time: [],
      load: false,
      loadmsg: false,
    });
    //#region 操作方法
    //请求数据
    onMounted(() => {
      //获取桌号列表
      getTablelist();
      //获取订单数据
      getdatalist();
    });
    async function getTablelist() {
      try {
        const obj = {table_name: null, table_number: null};

        const res = await new proxy.$request(
          proxy.$urls.m().gettable,
          obj
        ).modePost();
        console.log(res);
        datas.options = res.data.tables;

        //console.log(opendata);
      } catch (e) {
        new proxy.$tips(e, "error").mess_age();
      }
    }

    async function getdatalist() {
      try {
        let start = null;
        let end = null;
        if (datas.order_time.length > 0) {
          console.log(datas.order_time);

          start = new Date(datas.order_time[0]);
          
          end = new Date(datas.order_time[1]);
         
        }
        const obj = {
          page: datas.page,
          table_number: datas.sevakey,
          startTime: start,
          endTime: end,
        };
        const res = await new proxy.$request(
          proxy.$urls.m().obtainorder,
          obj
        ).modePost();
        //console.log(res);
        datas.data_table = res.data.orders;
        datas.totalCount = res.data.total;
        datas.load = false;
        //console.log(opendata);
      } catch (e) {
        datas.load = false;
        new proxy.$tips(e, "error").mess_age();
      }
    }
    //页数切换
    function current_change(number) {
      //console.log(number);
      datas.page = number;
      getdatalist();
    }
    //详细菜单
    async function Detailed_Menu(index, data) {
     
      try {
        datas.loadmsg = true;
        const res = await new proxy.$request(
          proxy.$urls.m().viewoder + "?id=" + data.id
        ).modeGet();
        datas.loadmsg = false;
        //console.log(res);
        orderLog.value.popup(res.data);
      } catch (e) {
        datas.loadmsg = false;
        new proxy.$tips(e, "error").mess_age();
      }
    }
    //provide('value',res)
    //付款
    function Check_Out(index, data) {
      console.log({index, data});
    }
    //查询菜单
    function Searchmenu() {
      datas.load = true;
      getdatalist();
    }

    //时间格式

    function formatTime(row, column) {
      // 使用 JavaScript 内置的 Date 对象将字符串日期转换为 Date 对象
      const date = new Date(row[column.property]);
      // 使用 toLocaleString 方法将日期格式化为所需的格式，例如中国的日期格式为 "yyyy-MM-dd HH:mm:ss"
      return date.toLocaleString("zh-CN", {timeZone: "Asia/Shanghai"});
    }

    provide("orderdata", datas.orderlist);

    //#endregion
    return {
      ...toRefs(datas),
      current_change,
      Detailed_Menu,
      Check_Out,
      Searchmenu,
      formatTime,
      orderLog,
    };
  },
};
</script>

<style scoped="scoped">
.demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}

::v-deep .el-table__cell {
  /* 添加您需要的样式 */
  position: static !important;
}

::v-deep .el-pagination.is-background .el-pager li.is-active {
  background-color: rgb(18, 175, 18) !important;
  color: white;
}

.pagination-container {
  display: flex;
  justify-content: center; /* 将内容水平居中 */
}
</style>
